package com.fer.tankr.models.enemies;

import org.andengine.entity.shape.IShape;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;

public class CoinerEnemy extends Enemy{

	static int SLEEPING = 0;
	static int ACTIVE = 1;
	
	public IShape entityToFollow;
	public ITextureRegion sleepingTexture;
	public ITextureRegion activeTexture;
	
	private int state;
	
	public CoinerEnemy(PhysicsWorld world, VertexBufferObjectManager vbom,
			float x, float y, IShape entityToFollow, ITextureRegion coinTexture, ITextureRegion coinFuckerTexture) {
		super(world, vbom, x, y, 30, 30, 3);
		this.entityToFollow = entityToFollow;
		
		this.sleepingTexture = coinTexture;
		this.activeTexture = coinFuckerTexture;
		attachSprite(coinTexture, 0, 0).setAlpha(0.5f);
		state = SLEEPING;
	}
	

	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		
		float x = entityToFollow.getX();
		float y = entityToFollow.getY();
		
		float width = this.entity.getWidth();
		float height = this.entity.getHeight();
		
		if (Math.abs(x-this.entity.getX())<5*width && Math.abs(y-entity.getY()) < 5*height)
		{
			if (state == SLEEPING)
			{
				entity.detachChildren();
				attachSprite(activeTexture, 0, 0);
			}
			state = ACTIVE;
			move();
		}
		else
		{
			if (state == ACTIVE)
			{
				entity.detachChildren();
				attachSprite(sleepingTexture, 0, 0);
			}
			state = SLEEPING;
		}
	}

	private void move() {
		float x = entityToFollow.getX()-entity.getX();
		float y = entityToFollow.getY()-entity.getY();
		Vector2 v = new Vector2(x, y).nor().mul(4.5f);
		body.setLinearVelocity(v);
	}

	
}





